﻿using System;
using System.Windows.Forms;
using System.IO;
using MediaController.Client.DAL;

namespace MediaController.Client.Controls.Dialogs
{
    public partial class WatchFolderDlg : Form
    {
        public string Source
        {
            get { return _textBoxSource.Text; }
        }

        public int Depth
        {
            get
            {
                string text = string.IsNullOrEmpty(_maskedTextBoxDepth.Text) ? "1" : _maskedTextBoxDepth.Text;
                return Convert.ToInt32(text);
            }
        }

        public string Destination
        {
            get { return string.IsNullOrEmpty(_textBoxSource.Text) ? Source : _textBoxSource.Text; }
        }

        public bool MoveExtractedFiles
        {
            get { return _checkBoxMoveExtractFile.Checked; }
        }

        public bool MoveWithDirectory
        {
            get { return checkBoxMoveWithDirectory.Checked; }
        }

        public string Execute
        {
            get { return _textBoxExecute.Text; }
        }

        public WatchFolderDlg(BindingSource bindingSource)
        {
            InitializeComponent();

            bindingSource1 = bindingSource;
            _groupBoxDestination.Hide();
        }

        private void ButtonBrowesClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(_textBoxSource.Text))
                _folderBrowserDialog.SelectedPath = _textBoxSource.Text;

            if (_folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                _textBoxSource.Text = _folderBrowserDialog.SelectedPath;
            }
        }

        private void ButtonBrowseDestinationClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(_textBoxDestination.Text))
                _folderBrowserDialog.SelectedPath = _textBoxDestination.Text;

            if (_folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                _textBoxDestination.Text = _folderBrowserDialog.SelectedPath;
            }
        }

        private void ButtonChoseFileClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(_textBoxExecute.Text))
                _openFileDialogExecute.InitialDirectory = Path.GetDirectoryName(_textBoxExecute.Text);

            if (_openFileDialogExecute.ShowDialog() == DialogResult.OK)
            {
                string file = _openFileDialogExecute.FileName;
                string extention = Path.GetExtension(file);
                if (extention.ToLower() == ".exe" || extention.ToLower() == ".bat")
                    _textBoxExecute.Text = file;
            }
        }

        private void ButtonOkClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Source))
            {
                MessageBox.Show(string.Format("You must souce directory"));
            }
            else if (!Directory.Exists(Source))
            {
                MessageBox.Show(string.Format("Cannot find directory {0}", Source));
            }
            else
            {
                var mediaControllerDataSet = DataAccess.Instance.MediaControllerDataSet;
                var listenersRow = mediaControllerDataSet.Listeners.NewListenersRow();

                listenersRow.Source = Source;
                listenersRow.Filter = "*.rar;*.001";
                listenersRow.Move = MoveExtractedFiles;
                listenersRow.Execute = Execute;
                //listenersRow.Enabled = true;
                mediaControllerDataSet.Listeners.AddListenersRow(listenersRow);
            }
        }

        private void ButtonCancelClick(object sender, EventArgs e)
        {

        }

        private void WatchFolderDlg_Load(object sender, EventArgs e)
        {

        }

        private void MaskedTextBoxDepthEnter(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_maskedTextBoxDepth.Text))
            {
                _maskedTextBoxDepth.Select(0, 1);
                _maskedTextBoxDepth.ScrollToCaret();
            }
        }


    }
}
